home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / delphi1 / lessons.exe / lesson2 / CASEUNIT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-12-13  |  2.5 KB  |  92 lines

  1. unit Caseunit;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls;
  8.  
  9. type
  10.   TCaseTestForm = class(TForm)
  11.     ConvertFromEd: TEdit;
  12.     ConvertToEd: TEdit;
  13.     Label1: TLabel;
  14.     Label2: TLabel;
  15.     CalcBtn: TButton;
  16.     GroupBox1: TGroupBox;
  17.     KmToMilesRBtn: TRadioButton;
  18.     MilesToKmRBtn: TRadioButton;
  19.     CToFRBtn: TRadioButton;
  20.     FToCRBtn: TRadioButton;
  21.     procedure CalcBtnClick(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.   public
  25.     { Public declarations }
  26.   end;
  27.  
  28. var
  29.   CaseTestForm: TCaseTestForm;
  30.  
  31. implementation
  32.  
  33. {$R *.DFM}
  34.  
  35. function NoError( Code : integer ) : boolean;
  36. { an example of using a case statement as an alternative to
  37.   multiple if-then-else conditions }
  38. var
  39.    ErrMsg : string;
  40. begin
  41. { tests the error code returned by VAL }
  42.   case Code of
  43.     1 : ErrMsg := 'First character is not a number';
  44.     2, 3 : ErrMsg := 'Error found at position 2 or 3';
  45.     4..10: ErrMsg := 'Error found somewhere between 4 and 10';
  46.   else
  47.     ErrMsg := 'Error at position 11 or above';
  48.   end;
  49. { if Code = 0 then VAL found no errors and the NoError function returns a
  50.   value of True. Otherwise, if Code is some other value, a message dialog
  51.   displays the ErrMsg assigned by the CASE statement above and the
  52.   NoError function returns False. }
  53.   if Code <> 0 then
  54.   begin
  55.       MessageDlg(ErrMsg, mtWarning, [mbOK], 0 );
  56.       NoError := false;
  57.   end
  58.   else
  59.      NoError := true;
  60. end;
  61.  
  62. procedure TCaseTestForm.CalcBtnClick(Sender: TObject);
  63. const
  64.      milesToKM = 1.60934;
  65.      KMToMiles = 0.62137;
  66. var
  67.    InputVal, OutputVal : Real;
  68.    Code : integer;
  69.    S : string;
  70. begin
  71.    Val(ConvertFromEd.Text, InputVal, Code );
  72.    { Calls the NoError function to test the Code variable returned by VAL.
  73.    If there is no error, the calculation between the following begin-end pair
  74.    is performed. If there is an error, the lines between the begin-end pair
  75.    are skipped } 
  76.    If NoError( Code ) Then
  77.    begin  { - start of the bracketing begin-end pair - }
  78.      If KmToMilesRBtn.checked then
  79.          OutputVal := InputVal * KmToMiles
  80.      else if MilesToKmRBtn.checked then
  81.          OutputVal := InputVal * milesToKM
  82.      else if CToFRBtn.checked then
  83.          OutputVal := ((InputVal * 9) / 5) + 32
  84.      else if FToCRBtn.checked then
  85.          OutputVal := ((InputVal -32) * 5) /9;
  86.      Str( OutputVal:2:2, S );
  87.      ConvertToEd.Text :=  S;
  88.    end; { - end of the bracketing begin-end pair - }
  89. end;
  90.  
  91. end.
  92.